package org.alex.sample.dao.Impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.alex.sample.VO.SubjectVO;
import org.alex.sample.VO.SubjectVOToTest;
import org.alex.sample.VO.UserVO;
import org.alex.sample.dao.StudentDAO;
import org.alex.sample.dao.SubjectDAO;
import org.alex.sample.model.ExamScore;
import org.alex.sample.model.ExamTest;
import org.alex.sample.model.ExamUserRole;
import org.alex.sample.model.ExamUserSubject;
import org.alex.sample.model.Subject;
import org.alex.sample.model.User;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.StringType;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository
public class StudentDAOImpl extends BaseHibernateDAO<User> implements StudentDAO {
	
	@Autowired
	private SubjectDAO subjectDao;

	//根据ID查找学生，并保存到拓展类里
	@Override
	public UserVO findStudentById(Integer userId) throws Exception {
		User user = this.get(userId);
		UserVO userVO = new UserVO();
		BeanUtils.copyProperties(user, userVO);
		userVO.setGender(user.getGender().equals("0")?"男":"女");
		for(ExamUserRole examUserRole:user.getExamUserRole()) {
			userVO.setRoleName(examUserRole.getExamRole().getName());
			userVO.setRoleId(examUserRole.getExamRole().getRoleId());
		}
		List<SubjectVO> subjectVOList = new ArrayList<SubjectVO>();
		for(ExamUserSubject examUserSubject : user.getExamUserSubject()) {
			SubjectVO subjectVO = new SubjectVO();
			BeanUtils.copyProperties(examUserSubject.getSubject(), subjectVO);
			subjectVO.setStatus(examUserSubject.getSubject().getStatus());
			for(ExamScore examScore : subjectVO.getExamScore()) {
				subjectVO.setScoreId(examScore.getScoreId());
				subjectVO.setScoreExamTime(examScore.getExamTime());
				subjectVO.setScore(examScore.getScore());
			}
			subjectVOList.add(subjectVO);
		}
		userVO.setSubjectVOList(subjectVOList);
		return userVO;
	}
	
	//根据要考试的科目ID来查找考试题
	@Override
	public SubjectVOToTest findAllTestBySubjectId(Integer subId) throws Exception{
		Subject subject = subjectDao.findSubjectById(subId);
		List<ExamTest> examTestList = new ArrayList<ExamTest>();
		for(ExamTest examTest : subject.getExamTest()) {
			examTestList.add(examTest);
		}
		SubjectVOToTest subjectVOTotest = new SubjectVOToTest();
		BeanUtils.copyProperties(subject, subjectVOTotest);
		subjectVOTotest.setExamTestList(examTestList);
		return subjectVOTotest;
	}
	
	//保存分数到数据库，保存前进行查找数据库有没有该科目分数，防止多次提交
	@Override
	public Map<String, Object> saveExamScore(double score, Integer subId, Integer userId) throws Exception{
		@SuppressWarnings("unchecked")
		List<ExamScore> examScoreList = this.createSqlQuery("select id as scoreId,exam_time as examTime,score,user_id as userId,"
				+ "sub_id as subId from exam_score where sub_id=" + subId + " and user_id=" + userId)
				.addScalar("score", StandardBasicTypes.DOUBLE) //设置查询到的成绩类型为double
				.setResultTransformer(new AliasToBeanResultTransformer(ExamScore.class)).list();
		Map<String, Object> messages = new HashMap<String, Object>();
		if(examScoreList.size() > 0) {
			messages.put("success", false);
		}else {
			String sql = "";
			sql += "insert into exam_score(user_id,sub_id,exam_time,score) value(";
			sql += userId + "," + subId + ",'" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "'," + score + ")";
			this.createSqlQuery(String.valueOf(sql)).executeUpdate();
			messages.put("success", true);
		}
		return messages;
	}
	
	@Override
	public Iterator<User> iterate(String queryString, Object[] values) {
		return null;
	}
}
